package com.hs.mobile.dao.device;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.hs.mobile.model.Category;
import com.hs.mobile.model.Device;
import com.hs.mobile.model.ServiceMB;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class DeviceDAOImpl implements DeviceDAO {

	SessionFactory sessionFactory;
	
	//setter sessionFactory
	/**
	 * 
	 * @param sessionFactory
	 */
	public void setSessionFactory( SessionFactory sessionFactory )
	{
		this.sessionFactory = sessionFactory;
	}
	
	//getter sessionFactory
	/**
	 * 
	 * @return sessionFactory
	 */
	public SessionFactory getSessionFactory()
	{
		return sessionFactory;		
	}
	@Override
	public List getListAll() {
		// TODO Auto-generated method stub
		System.out.println("__ Get list All Devices that are using this service __" );
		Session session = getSessionFactory().openSession();
		List<Device> listDevices = null;
		try {
			Criteria cr = session.createCriteria(Device.class);
			listDevices = cr.list();
			System.out.println("__ get listDevices successfully __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return listDevices;
	}

	@Override
	public Object getById(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ Get Device __");
		Session session = getSessionFactory().openSession();
		Device device = null;
		try {
			device = (Device) session.get(Device.class, id);
			System.out.println("__ Get Device successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return device;
	}

	@Override
	public void add(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Register Device __ ");
		Device device = ( Device ) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			System.out.println("__ register successfully __");
			session.save(device);
			System.out.println("__ register successfully __");
			session.getTransaction().commit();
			System.out.println("__ register successfully __");
			System.out.println("__ Your mobile can use this service from now __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}		
	}

	@Override
	public void delete(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Your mobile does not continue using this service ___");
		Device device = (Device) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.delete(device);
			session.getTransaction().commit();
			System.out.println("__ Delete device successful __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
	}

	@Override
	public void update(Object obj) {
		// TODO Auto-generated method stub
		System.out.println("__ Update __ ");
		Device device = (Device) obj;
		Session session = getSessionFactory().openSession();
		try {
			session.beginTransaction();
			session.update(device);
			session.getTransaction().commit();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
	}

	@Override
	public Device findByDeviceId(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ Get Device __");
		Session session = getSessionFactory().openSession();
		Device device = null;
		try {
			device = (Device) session.get(Device.class, id);
			System.out.println("__ Get Device successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return device;
	}

	@Override
	public void updateStatusDevice(long id, int status , Date modified) {
		// TODO Auto-generated method stub
		System.out.println("__ Your device was banded. Please re-regist to coninue using this service __");
		Session session = getSessionFactory().openSession();
		Device device = findByDeviceId(id);
		device.setStatus(status);
		try {
			session.beginTransaction();
			session.update(device);
			session.getTransaction().commit();
			System.out.println("__ update status successfully __ ");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
	}

	@Override
	public List<Device> getListDevicesByExpireDate(Date expireDate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Device> getListAllAvailableDevices() {
		// TODO Auto-generated method stub
		System.out.println("__ get all available devices __");
		Session session = getSessionFactory().openSession();
		List<Device>  listDevices = null;
		try {
			String sql = "SELECT * FROM DEVICE WHERE STATUS = 1";			
			SQLQuery query = session.createSQLQuery(sql);
	        query.addEntity(Device.class);
	        listDevices = query.list();
			System.out.println("__ get listDevices successfully __");
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return listDevices;
	}

	@Override
	public List<Device> getDevicesByCategoryId(long categoryId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Device> getDeviceByIdGCM(String idGCM) {
		// TODO Auto-generated method stub
		System.out.println("__ GetDevice that was registered on Google play or not __");
		Session session = getSessionFactory().openSession();
		List<Device> listDevices = null;
		try {
			String sql = "SELECT * FROM DEVICE WHERE ID_GCM = " + "'" + idGCM + "'";			
			SQLQuery query = session.createSQLQuery(sql);
	        query.addEntity(Device.class);
	        listDevices = query.list();			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return listDevices;
	}

	@Override
	public List<ServiceMB> getListServiceUserRegister(long id) {
		// TODO Auto-generated method stub
		System.out.println("__ GetDevice that was registered on Google play or not __");
		Session session = getSessionFactory().openSession();
		List<ServiceMB> listRegisteredServices = null;
		try {
			String sql = "SELECT * FROM SERVICE SV INNER JOIN REGISTER REG ON REGISTER ON SV.ID_CATEGORY = REG.ID_CATEGORY" +
					"INNER JOIN DEVICE DEV ON DEV.ID = REG.ID_CATEGORY WHERR DEVICE.ID = " + "'" + id + "'";
			SQLQuery query = session.createSQLQuery(sql);
	        query.addEntity(ServiceMB.class);
	        listRegisteredServices = query.list();			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("__ Error __");
		} finally {
			session.close();
		}
		return listRegisteredServices;
	}
}
